package com.caterina.components;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Image;
import java.awt.Rectangle;
import java.awt.Toolkit;
import java.awt.Window;
import java.lang.reflect.Method;

import com.apple.eawt.Application;

import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;

import person.Person;

import com.caterina.activityManager.CActivity;

import controllers.DataListener;
import controllers.ManagerTask;
import controllers.ManagerTask.TaskType;

public class CFrame extends JFrame implements DataListener{
	public CFrame(CActivity activity){
		super(CFrame.APP_NAME+" - "+activity.getTitle());
		super.setBounds(CFrame.getSafetyBounds());
		instance=this;
		String osName=System.getProperty("os.name");
		super.setDefaultCloseOperation(EXIT_ON_CLOSE);
		super.setMinimumSize(new Dimension(1024,600));
		super.setBackground(Color.white);
		if(osName.equalsIgnoreCase("Mac OS X")){
			Application application = Application.getApplication();
			Image image = Toolkit.getDefaultToolkit().getImage("graphics/icon.png");
			application.setDockIconImage(image);
			System.setProperty(
                    "com.apple.mrj.application.apple.menu.about.name",
                    "Full Screen Demo");
			application.requestToggleFullScreen(new Window(this));
			enableFullScreenMode(this);
		}else{
			ImageIcon icon=new ImageIcon("graphics\\icon.png");
			icon=new ImageIcon(icon.getImage().getScaledInstance(16, 16, Image.SCALE_SMOOTH));
			super.setIconImage(icon.getImage());
		}
		mainPanel=new JPanel(new BorderLayout());//Pannello che contiene la barra e il contenuto
		subPanel=new JPanel(new BorderLayout());//Pannello che contiene il contenuto(in main Panel)
		subPanel.setBackground(Color.white);
		subPanel.setLayout(new BorderLayout());
		activity.setParent(subPanel);
		mainPanel.add(subPanel,BorderLayout.CENTER);
		this.caterinaBar=new CBar(activity.getTitle(),activity.getActivityManager(),subPanel);
		mainPanel.add(caterinaBar,BorderLayout.NORTH);
		super.add(mainPanel);
		this.putActivity(activity);
	}
	public void putActivity(CActivity activity){ 
		this.prevous=this.activity;
		if (prevous!=null) {
			this.prevous.onStop();
			subPanel.remove(prevous);
		}
		this.activity=activity;
		this.start();
	}
	public CActivity getCurrentActivity(){
		return this.activity;
	}
	public void start(){
		super.setTitle(CFrame.APP_NAME+" - "+activity.getTitle());
		this.caterinaBar.setVisible(activity.isCBarVisible());
		this.caterinaBar.setTitle(activity.getTitle());
		if (activity.isCBarQueryesEnabled()){
			this.caterinaBar.setRightText("Loading");
			activity.getDataController().putTask(new ManagerTask(TaskType.USER_CURRENT, this));
		}else this.caterinaBar.setRightText("");
		this.caterinaBar.setActivityManager(activity.getActivityManager());
		this.caterinaBar.setLogoutButtonVisible(activity.isLogoutButtonVisible());
		this.caterinaBar.setSbButtonVisible(activity.isSbButtonVisible());
		this.subPanel.add(activity,BorderLayout.CENTER);
		activity.startLoading();
		activity.onStart();
		this.subPanel.repaint();
		this.subPanel.revalidate();
		this.mainPanel.repaint();
		this.mainPanel.revalidate();
		super.repaint();
		super.validate();
	}
	public void setCbarTitle(String title){
		this.caterinaBar.setTitle(title);
	}
	public void setCbarRightText(String right){
		this.caterinaBar.setRightText(right);
	}
	/**
	 * @return
	 */
	public CActivity getActivity(){
		return this.activity;
	}
	@Override
	public void onData(TaskType task, Object data) {
		switch(task){
			case USER_CURRENT:{
				Person pers=(Person) data;
				caterinaBar.setRightText(pers.getName()+" "+pers.getSurname());
			}
		default:break;
		}
	}
	@Override
	public void onError(TaskType task, Exception e) {
		if (!(this.activity.getName().equalsIgnoreCase("login"))){
			caterinaBar.setRightText("Error: "+e.getMessage());
			JOptionPane.showMessageDialog(this, e.getMessage());
		}
	}
	private static void enableFullScreenMode(Window window) {
        String className = "com.apple.eawt.FullScreenUtilities";
        String methodName = "setWindowCanFullScreen";
 
        try {
            Class<?> clazz = Class.forName(className);
            Method method = clazz.getMethod(methodName, new Class<?>[] {
                    Window.class, boolean.class });
            method.invoke(null, window, true);
        } catch (Throwable t) {
            System.err.println("Full screen mode is not supported");
            t.printStackTrace();
        }
    }
	public static Rectangle getSafetyBounds() {
		if (instance!=null)return instance.getBounds();
		else return CFrame.getDefaultBounds();
	}
	private static Rectangle getDefaultBounds(){
		Dimension screenSize = Toolkit.getDefaultToolkit ( ).getScreenSize ();
		int w=1024,h=600;
		int x=(screenSize.width/2 )-(w/2);
		int y=(screenSize.height/2 )-(h/2);
		return new Rectangle(x,y,w,h);
	}
	
	public JPanel getMainPanel(){
		return this.mainPanel;
	}
	private static CFrame instance;
	private CActivity activity;
	private CActivity prevous;
	private static final long serialVersionUID = 1L;
	private JPanel mainPanel;
	private JPanel subPanel;
	private CBar caterinaBar;
	private static final String APP_NAME="Caterina";
}
